package com.jindy.module.oaLeaveApproval.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.oaLeaveApproval.domain.SyOaLeaveApproval;
import com.jindy.module.oaLeaveApproval.service.ISyOaLeaveApprovalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 请假审批Controller
 * 
 * @author jindy
 * @date 2023-05-16
 */
@RestController
@RequestMapping("/module/oaLeaveApproval")
public class SyOaLeaveApprovalController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(SyOaLeaveApprovalController.class);
    @Autowired
    private ISyOaLeaveApprovalService syOaLeaveApprovalService;

    /**
     * 查询请假审批列表
     */
    @PreAuthorize("@ss.hasPermi('module:oaLeaveApproval:list')")
    @GetMapping("/list")
    public TableDataInfo list(SyOaLeaveApproval syOaLeaveApproval)
    {
        startPage();
        List<SyOaLeaveApproval> list = syOaLeaveApprovalService.selectSyOaLeaveApprovalList(syOaLeaveApproval);
        return getDataTable(list);
    }

    /**
     * 导出请假审批列表
     */
    @PreAuthorize("@ss.hasPermi('module:oaLeaveApproval:export')")
    @Log(title = "请假审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SyOaLeaveApproval syOaLeaveApproval)
    {
        List<SyOaLeaveApproval> list = syOaLeaveApprovalService.selectSyOaLeaveApprovalList(syOaLeaveApproval);
        ExcelUtil<SyOaLeaveApproval> util = new ExcelUtil<SyOaLeaveApproval>(SyOaLeaveApproval.class);
        util.exportExcel(response, list, "请假审批数据");
    }

    /**
     * 获取请假审批详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:oaLeaveApproval:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(syOaLeaveApprovalService.selectSyOaLeaveApprovalById(id));
    }

    /**
     * 新增请假审批
     */
    @PreAuthorize("@ss.hasPermi('module:oaLeaveApproval:add')")
    @Log(title = "请假审批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SyOaLeaveApproval syOaLeaveApproval)
    {
        return toAjax(syOaLeaveApprovalService.insertSyOaLeaveApproval(syOaLeaveApproval));
    }

    /**
     * 修改请假审批
     */
    @PreAuthorize("@ss.hasPermi('module:oaLeaveApproval:edit')")
    @Log(title = "请假审批", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SyOaLeaveApproval syOaLeaveApproval)
    {
        return toAjax(syOaLeaveApprovalService.updateSyOaLeaveApproval(syOaLeaveApproval));
    }

    /**
     * 删除请假审批
     */
    @PreAuthorize("@ss.hasPermi('module:oaLeaveApproval:remove')")
    @Log(title = "请假审批", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(syOaLeaveApprovalService.deleteSyOaLeaveApprovalByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "外出审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply/{id}")
    @ResponseBody
    public AjaxResult submitApply(@PathVariable Long id) {
        try {
            SyOaLeaveApproval syOaLeaveApproval = syOaLeaveApprovalService.selectSyOaLeaveApprovalById(id);
            syOaLeaveApprovalService.submitApply(syOaLeaveApproval);
        } catch (Exception e) {
            LOGGER.error("请假审批提交申请出错",e);
            return error("请假审批提交申请出错：" + e.getMessage());
        }
        return success();
    }
}
